package com.songdehuai.supermarket.presenter.imp;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.songdehuai.supermarket.config.Apis;
import com.songdehuai.supermarket.presenter.SuperMarketPresenter;
import com.songdehuai.supermarket.result.Result;
import com.songdehuai.supermarket.supermarket.entity.SuperMarket;
import com.songdehuai.supermarket.sync.SuperMarketSync;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;

public class SuperMarketPresenterImp implements SuperMarketPresenter {

    private Activity activity;
    private SuperMarketSync sync;

    public SuperMarketPresenterImp(Activity activity, SuperMarketSync sync) {
        this.activity = activity;
        this.sync = sync;
    }

    @Override
    public void getSuperMarket(boolean isMore, String id) {
        RequestParams requestParams = new RequestParams(Apis.SHOP_GOODS(id));
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                Type type = new TypeToken<Result<SuperMarket>>() {
                }.getType();
                Result<SuperMarket> superMarketResult = gson.fromJson(result, type);
                if (superMarketResult.getCode() == 0) {
                    sync.superMarketSuccess(superMarketResult);
                } else {
                    sync.showTost("错误");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
